﻿using Bussiness.Interface;
using Bussiness.Service;
using Common.Model;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ShopingCar : System.Web.UI.Page
{
    public List<Orders> orders = new List<Orders>();
    public List<Orders> LikeOrders = new List<Orders>();
    Users user = new Users();
    BookService bookService = new BookService();
    public IBaseService baseService = new BaseService();
    protected void Page_Load(object sender, EventArgs e)
    {
        user = (Users)Session["user"];
        Result<List<Orders>> listOrders = new Result<List<Orders>>();
        Result<List<Orders>> LikelistOrders = new Result<List<Orders>>();
        listOrders = baseService.Select<Orders>(p => p.UserID == user.UserID, p => p.BookingStatus == Common.Enums.BookingStatus.JoinCar.ToString());
        if (listOrders.Data != null)
        {
            try
            {
                orders = listOrders.Data;

            }
            catch (Exception ex)
            {
                orders = null;
                Response.Redirect("ShopingCar.aspx");
            }
        }
        LikelistOrders = baseService.Select<Orders>(p => p.UserID == user.UserID, p => p.BookingStatus == Common.Enums.BookingStatus.Like.ToString());
        if (LikelistOrders.Data != null)
        {
            try
            {
                LikeOrders = LikelistOrders.Data;
            }
            catch
            {
                LikeOrders = null;
            }
        }
        if (Request.Params["bookid"] != "")
        {
            operation();
        }
    }
    protected void Unnamed_ServerClick2(object sender, EventArgs e)//去结算按钮
    {
        if (Session["user"] != null)
        {
                foreach (var item in orders)//循环符合条件的图书ID
                {
                    Orders ober = new Orders()
                    {
                        OrderID = item.OrderID,
                        BookID = item.BookID,
                        BookingTime = DateTime.Now,
                        BookingStatus = Common.Enums.BookingStatus.Success.ToString(),
                        UserID = item.UserID,
                        Num = item.Num,
                        Price = item.Price
                    };
                    //Common.Enums.BookingStatus.Pay;
                    if (baseService.Update(ober).Data)
                    {
                    }
                }
                Response.Redirect("ShopingCar.aspx");
            }
            else
            {
                Response.Write("<script>alert('付款失败')<script>");
            }
    }
    private void operation()//添加、删除、结算
    {
        if (Request.Params["bookid"] != "" && Request.Params["biaoshi"] == "add")
        {
            if (orders.Count > 0)
            {
                foreach (var item in orders)
                {
                    if (Request.Params["bookid"] == item.BookID)
                    {
                        Orders orders = new Orders()
                        {
                            BookID = item.BookID,
                            UserID = item.UserID,
                            Price = item.Price,
                            OrderID = item.OrderID,
                            Num = item.Num,
                            BookingStatus = Common.Enums.BookingStatus.Like.ToString(),
                            BookingTime = DateTime.Now
                        };
                        if (baseService.Update(orders).Data)
                        {
                            Response.Write("<script>alert('添加到愿望单成功')</script>");
                            return;
                        }
                        else
                        {
                            Response.Write("<script>alert('添加到愿望单失败')</script>");
                        }
                    }
                }
            }
        }
    }
}